package com.chenliang.invoke;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chenliang.invoke.utils.HttpUtils;
import org.apache.commons.codec.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import springfox.documentation.spring.web.json.Json;

import java.util.*;


/**
 * @program: long_invoke
 * @description:
 * @author: liangchen23
 * @create: 2022-03-15 16:47
 */
public class InvokeDemo {


    public static void main(String[] args) {



        HttpClient client = HttpClients.createDefault();
        // 要调用的接口方法
        String url = "http://127.0.0.1:8080/user/get";
        HttpPost post = new HttpPost(url);
        JSONObject jsonObject = null;
        try {

            Map<String,String> map= new HashMap<>();
            map.put("id","1");
            List<NameValuePair> nvps = new ArrayList<>();
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                nvps.add(new BasicNameValuePair(key, map.get(key)));
            }


            post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            post.addHeader("content-type", "text/xml");
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                jsonObject = JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("远程调用JSONObject jsonObject返回的结果:" + jsonObject);

        Map<String,Object> map= new HashMap<>();
        map.put("id","1");
        String sendxwwwform = HttpUtils.sendxwwwform(url, map);
        System.out.println(sendxwwwform);
    }
}
